const {
  getCheckpointList,
  saveCheckpointList,
  getCheckpointByID
} = require("initdata")

export const DefaultItemType = 201
export const RightItemType = 202
export const RedoItemType = 203

export function getNextFlowItemByType(item, type) {
  if (item.item_list !== undefined) {
    for (let index = 0; index < item.item_list.length; index++) {
      if (item.item_list[index].type === type) {
        return item.item_list[index]
      }
    }
  } else {}

  return false
}

// 判断用户回答是否正确，并引导交互
export function doEndTask(checkpoint_list, checkpoint, task, answer) {
  let flow_item = {}

  if (task.item_list !== undefined) { // 存在分支，提取分支
    // console.log('分支', task)
    if (answer === task.answer) { // 回答正确
      console.log('回答正确')
      flow_item = getNextFlowItemByType(task, RightItemType)
      console.log('正确分支', flow_item)
      if (flow_item) {
        // 保存下一步流程
        checkpoint.workflow.flow = flow_item.flow
        checkpoint.workflow.step = 0
        saveCheckpointList(checkpoint_list)

        // 导航到task
        wx.redirectTo({
          url: '../main/task?id=' + checkpoint.code,
        })

        return
        // 提示用户
        // return wx.showModal({
        //   title: '提示',
        //   content: flow_item.prompt === '' ? '恭喜你答对了' : flow_item.prompt,
        //   success(res) {
        //   }
        // })
      }

      // step 3：出错了！
      console.log('错误，请检查游戏配置')

    } else { // 回答错误
      console.log('回答错误')
      // step 1: 重玩分支
      flow_item = getNextFlowItemByType(task, RedoItemType)
      console.log('REDO分支', flow_item)
      if (flow_item) {
        return wx.showModal({
          title: '提示',
          content: flow_item.prompt === '' ? '很遗憾，回答错误。' : flow_item.prompt,
          complete: (res) => {
            if (res.cancel) {

            }

            if (res.confirm) {

            }
          }
        });
      }
      // step 2：默认分支
      flow_item = getNextFlowItemByType(task, DefaultItemType)
      console.log('错误分支', flow_item)
      if (flow_item) {
        return wx.showModal({
          title: '提示',
          content: flow_item.prompt === '' ? '很遗憾，回答错误。' : flow_item.prompt,
          complete: (res) => {
            // 保存下一步流程
            checkpoint.workflow.flow = flow_item.flow
            checkpoint.workflow.step = 0
            // console.log(checkpoint_list, checkpoint)
            saveCheckpointList(checkpoint_list)

            // 导航到task
            wx.redirectTo({
              url: '../main/task?id=' + checkpoint.code,
            })
          }
        });
      }

      // step 3：出错了！
      console.log('错误，请检查游戏配置')
    }
  } else { // 没有分支，直接执行
    console.log('没有分支')
    if (answer === task.answer) { // 回答正确
      console.log('回答正确')
      // 保存流程数据
      checkpoint.workflow.step++
      saveCheckpointList(checkpoint_list)

      // 导航到task
      wx.redirectTo({
        url: '../main/task?id=' + checkpoint.code,
      })
    } else { // 回答错误
      console.log('回答错误')
      return wx.showModal({
        title: '提示',
        content: '很遗憾，回答错误。',
        complete: (res) => {
          if (res.cancel) {

          }

          if (res.confirm) {

          }
        }
      });
    }
  }
}